<?php
namespace Amoysea\Controller;
use Think\Controller;
class BookController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
        $db_name=$g['db'];
        $this->product = M('book',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
    }

    public function index(){
        if(!isset($_GET['p']) || $_GET['p']==''){
            $p=1;
        }else{
            $p=intval($_GET['p']);
        }
        if(I("get.start_time")!='') {
            $where['created_at']= array('gt',I("get.start_time"));
        }
        if(I("get.end_time")!=''){
            $where['created_at'] = array('lt',I("get.end_time"));
        }
        if(I("get.start_time")!='' && I("get.end_time")!=''){
            $where['created_at']= array(array('gt',I("get.start_time")),array('lt',I("get.end_time")));
        }
        if(I("get.name")!=''){
            $where['name']=array('like',"%".I("get.name")."%");
        }
        if(I("get.page_count")=='' || I("get.page_count")<=0){
            $page_count=15;
        }else{
            $page_count=intval(I("get.page_count"));
        }
        if(I("get.sort")!=''){
            $order=I("get.sort")." ".I("get.dec");
        }else{
            $order='id desc';
        }
        if(!isset($where)){
            $where="1=1";
        }
        $list = $this->product->where($where)->order($order)->page($p.','.$page_count)->select();
        $this->assign('list',$list);
        $count      = $this->product->where($where)->count();
        $this->assign('count',$count);
        $Page       = new \Think\Page($count,$page_count);
        $show       = $Page->show();
        $this->assign('page',$show);
        $this->display();
    }
    public function add(){
        $this->display();
    }
    public function addD($name,$book_no,$type,$merchant_code){
        if($name==''){
            alert(0,'账册名称不能为空');
        }
        if($book_no==''){
            alert(0,'账册编号不能为空');
        }
		if($merchant_code==''){
            alert(0,'商家编号不能为空');
        }
        if($type!=1 && $type!=2){
            alert(0,'请选择账册类型');
        }
        $data['name']=$name;
        $data['book_no']=$book_no;
		$data['merchant_code']=$merchant_code;
        $data['type']=$type;
        $massage=session('company');
        $data['operator']=$massage['username'];
        if($this->product->where(array('name'=>$name))->count()>0){
            alert(0,'账册名称不能重复');
        }
        if($this->product->where(array('book_no'=>$book_no))->count()>0){
            alert(0,'账册编号不能重复');
        }
        $result=$this->product->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
    public function getWarehouse(){
        $g=session('company');
        $user_id=$g['company_id'];
        $group_id=M("user_warehouse")->where(array('user_id'=>$user_id))->getField('warehouse_group_id');
        $group_id=explode(',',$group_id);
        $where['id']=array('in',$group_id);
        $result=M("warehouse")->where($where)->getField('id,name',true);
        if($result){
            alert(1,$result);
        }else{
            alert(0,'暂无仓库信息');
        }
    }
    public function push($id,$warehouse_id){
        $g=session('company');
        $db_name=$g['db'];
        $user_id=$g['company_id'];
        $f=C('DB_PREFIX_T');
        $this->p = M('book',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $list=$this->p->where("id='".$id."'")->find();
        $list['erp_code']=$g['company_id'];
        //////如果已经推送则不能再次推送
        if($list['examined']>0){
           // alert(0,'请不要重复推送');
        }
        unset($list['examined']);
        $g_id=M("user_warehouse")->where(array('user_id'=>$user_id))->getField('warehouse_group_id');
        $group_id=explode(',',$g_id);
        if(!in_array($warehouse_id,$group_id)){
            alert(0,'操作有误');
        }
        $d=M("warehouse")->join("__INTERFACES__ on __INTERFACES__.warehouse_id=__WAREHOUSE__.id")->where(C('DB_PREFIX')."warehouse.id='".$warehouse_id."'")->getField('vars,url,warehouse_code');
        $url=$d['var_5']['url'];
        $data=array(
            'erp_code'=>$g['company_id'],
            'warehouse_code'=>$d['var_5']['warehouse_code'],
            'list'=>$list,
        );
        $true=push($url,$data);//推送过去
		$true=json_decode(trim($true,'﻿'),true);//替换掉utf8 BOM格式编码字符
        if($true['status']==1){
            if($this->product->where("id='".$id."'")->setField('examined',1)){
                alert(1,$true['info']);
            }else{
                alert(0,'推送成功，状态更新失败');
            }
        }else{
            alert(0,$true['info']);
        }
    }
    public function edit($id){
        if($id==''){
            echo "错误";
        }
        $this->result=$this->product->find($id);
        $this->display();
    }
    public function editD($id,$name,$book_no,$type,$merchant_code){
        if($name==''){
            alert(0,'账册名称不能为空');
        }
        if($book_no==''){
            alert(0,'账册编号不能为空');
        }
		if($merchant_code==''){
            alert(0,'商家编号不能为空');
        }
        if($type!=1 && $type!=2){
            alert(0,'请选择账册类型');
        }
        $data['name']=$name;
        $data['book_no']=$book_no;
		$data['merchant_code']=$merchant_code;
        $data['type']=$type;
        $where['name']=$name;
        $where['id']=array('neq',$id);
        if($this->product->where($where)->count()>0){
            alert(0,'账册名称不能重复');
        }
        $wheres['book_no']=$book_no;
        $wheres['id']=array('neq',$id);
        if($this->product->where($wheres)->count()>0){
            alert(0,'账册编号不能重复');
        }
        $massage=session('company');
        $data['operator']=$massage['username'];
		$data['examined']=0;
		
        $result=$this->product->where("id='".$id."'")->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
    public function del($id){
        ///////$id=1@2@3
        $group_id=explode('@',$id);
        $id=implode(',',$group_id);
        $result=$this->product->delete($id);
        if($result){
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else{
            alert(0,'删除失败');
        }
    }
}